package com.atguigu.spzx.manager.service.impl.product;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.atguigu.spzx.manager.mapper.product.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.product.ProductMapper;
import com.atguigu.spzx.manager.mapper.product.ProductSkuMapper;
import com.atguigu.spzx.manager.service.product.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.dto.product.ProductSaveDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.product.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;
    @Resource
    ProductDetailsMapper productDetailsMapper;
    @Resource
    ProductSkuMapper productSkuMapper;
    @Override
    public PageInfo<ProductVo> page(Integer pageNum, Integer pageSize, ProductDto productDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductVo> list = productMapper.findPage(productDto);
        return new PageInfo<>(list);
    }
    //新增商品
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ProductSaveDto productSaveDto) {
        //1、先保存product
        productMapper.save(productSaveDto);
        //2、再保存 轮播图列表：绑定spu
        Long productId = productSaveDto.getId();
        ProductDetails productDetails = new ProductDetails();
        //一对一映射的表可以使用相同的id值
        productDetails.setProductId(productId);
        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());
        productDetails.setId(productId);
        //保存轮播图列表
        productDetailsMapper.save(productDetails);
        //构建雪花算法id生成对象
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        //3、再保存 sku列表: 绑定spu
        productSaveDto.getProductSkuList().forEach(productSku -> {
            productSku.setProductId(productId);
            //生成商品的唯一编号
            productSku.setSkuCode(snowflake.nextIdStr());
            productSku.setSaleNum(0);
            productSku.setStatus(0);

            productSku.setSkuName(productSaveDto.getName()+"-"+productSku.getSkuSpec());

        });
        //批量保存sku列表
        productSkuMapper.batchSave(productSaveDto.getProductSkuList());
    }

    @Override
    public ProductSaveDto getById(Long id) {
        //根据Product id查询product
        ProductSaveDto dto = productMapper.findById(id);
        //根据Product id查询productDetails
        ProductDetails productDetails = productDetailsMapper.findById(id);
        //根据Product id查询productSku列表
        List<ProductSku> productSkus = productSkuMapper.findSkusByProductId(id);

        //组装成dto对象
//        ProductSaveDto dto = new ProductSaveDto();
//        BeanUtils.copyProperties(product , dto);
        dto.setDetailsImageUrls(productDetails.getImageUrls());
        dto.setProductSkuList(productSkus);
        return dto;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(ProductSaveDto productSaveDto) {
        //更新product
        productMapper.updateById(productSaveDto);
        //更新productDetails
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(productSaveDto.getId());
        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());

        productDetailsMapper.updateById(productDetails);
        //更新productSku
        productSaveDto.getProductSkuList().forEach(productSku -> {
            productSku.setSkuName(productSaveDto.getName()+"-"+productSku.getSkuSpec());
            productSkuMapper.updateById(productSku);
        });

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);

        productDetailsMapper.deleteById(id);

        productSkuMapper.deleteByProductId(id);

    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        productMapper.updateAuditStatus(id , auditStatus);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStatus(Long id, Integer status) {
        productMapper.updateStatus(id , status);
        productSkuMapper.updateStatus(id , status);
    }
}
